﻿using System.Collections.Generic;
using System.Linq;

namespace Susam
{
    interface IBooksOfEntity
    {
        IEnumerable<BookDisplay> GetBooksViaID(int id);
    }

    class BooksOfPerson : IBooksOfEntity
    {
        public IEnumerable<BookDisplay> GetBooksViaID(int id)
        {
            var ds = SusamDS.DataSet;

            var bookIDs =
                from jnc in ds.jncBookAuthor
                where jnc.AuthorID == id
                select jnc.BookID;

            bookIDs = bookIDs.Union(
                from jnc in ds.jncBookContributor
                where jnc.ContributorID == id
                select jnc.BookID);

            return DatabaseManager.Instance.ConvertBookIDsToBookDisplay(bookIDs);
        }
    }

    class BooksOfLanguage : IBooksOfEntity
    {
        public IEnumerable<BookDisplay> GetBooksViaID(int id)
        {
            var ds = SusamDS.DataSet;

            var bookIDs =
                from jnc in ds.jncBookLanguage
                where jnc.LanguageID == id
                select jnc.BookID;

            return DatabaseManager.Instance.ConvertBookIDsToBookDisplay(bookIDs);
        }
    }

    class BooksOfFileType : IBooksOfEntity
    {
        public IEnumerable<BookDisplay> GetBooksViaID(int id)
        {
            var ds = SusamDS.DataSet;

            var bookIDs =
                from jnc in ds.jncBookFileType
                where jnc.FileTypeID == id
                select jnc.BookID;

            return DatabaseManager.Instance.ConvertBookIDsToBookDisplay(bookIDs);
        }
    }

    class BooksOfCollection : IBooksOfEntity
    {
        public IEnumerable<BookDisplay> GetBooksViaID(int id)
        {
            var ds = SusamDS.DataSet;

            var bookIDs =
                from jnc in ds.jncBookCollection
                where jnc.CollectionID == id
                select jnc.BookID;

            return DatabaseManager.Instance.ConvertBookIDsToBookDisplay(bookIDs);
        }
    }
}
